package com.hb.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.PageInfo;
import com.hb.exception.HbExcetionEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;

import java.util.TreeMap;

@Data
@Builder
public class Result<T> {

	@Schema(description = "Response code : 1 = success / -1 = failure ")
	private int stat;

	@Schema(description = "Response message")
	private String msg;

	@Schema(description = "Response data")
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private T data;


	/**
	 * success
	 */
	public static Result success() {
		return Result.builder()
				.stat(Msg.SUCCESS.code)
				.msg(Msg.SUCCESS.value)
				.build();
	}

	public static Result success(Object data) {
		return builder()
				.stat(Msg.SUCCESS.code)
				.msg(Msg.SUCCESS.value)
				.data(data)
				.build();
	}

	public static Result success(String msg) {
		return builder()
				.stat(Msg.SUCCESS.code)
				.msg(msg)
				.build();
	}

	public static Result success(PageInfo<?> info) {
		TreeMap<String, Object> map = new TreeMap<>();
		map.put("total", info.getTotal());
		map.put("list", info.getList());
		return builder()
				.stat(Msg.SUCCESS.code)
				.msg(Msg.SUCCESS.value)
				.data(map)
				.build();
	}


	/**
	 * Custom message
	 */
	public static Result message(int stat, String msg) {
		return builder()
				.stat(stat)
				.msg(msg)
				.build();
	}

	public static Result message(int stat, String msg, final Object data) {
		return builder()
				.stat(stat)
				.msg(msg)
				.data(data)
				.build();
	}

	public static Result message(HbExcetionEnum hbExcetionEnum) {
		return builder()
				.stat(hbExcetionEnum.getCode())
				.msg(hbExcetionEnum.getMsg())
				.build();
	}

	/**
	 * error
	 */
	public static Result error() {
		return builder()
				.stat(Msg.ERROR.code)
				.msg(Msg.ERROR.value)
				.build();
	}

	public static Result error(final String msg) {
		return builder()
				.stat(Msg.ERROR.code)
				.msg(msg)
				.build();
	}


	public enum Msg {
		//success code
		SUCCESS(1, "success"),
		//error code
		ERROR(-1, "error");
		private int code;
		private String value;

		Msg(int code, String value) {
			this.code = code;
			this.value = value;
		}
	}

}
